package com.lzcu.maoyan.mapreduce.mapreduce.reduce;

import com.lzcu.maoyan.mapreduce.mapreduce.unit.StockWritable;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.yarn.server.nodemanager.Context;

import java.io.IOException;

/**
 * @author WangLiang
 * @date `2024/4/24`
 */
public class StockReducer extends Reducer<Text, DoubleWritable, NullWritable, StockWritable> {
    @Override
    protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
        Double maxPrice=Double.MIN_VALUE;
        for (DoubleWritable value : values) {
            double v = value.get();
            if(maxPrice<v){
                maxPrice=v;
            }
        }
        StockWritable stockWritable = new StockWritable();
        stockWritable.setStockName(key.toString());
        stockWritable.setMaxPrice(maxPrice);

        context.write(NullWritable.get(),stockWritable);
    }
}

